#include "stdafx.h"
#include "YProactorTokenOnRecvFrom.h"
#include "IUDPSession.h"
#include "IUDPSessionCallback.h"
#include "YSimpleNetwork/YEndPoint.h"

namespace ysimple_network
{
	YProactorTokenOnRecvFrom::YProactorTokenOnRecvFrom(void)
		: m_pCallback(nullptr)
		, m_pSession(nullptr)
	{
		
	}


	YProactorTokenOnRecvFrom::~YProactorTokenOnRecvFrom(void)
	{
	}

	void YProactorTokenOnRecvFrom::beginInit( IRecvFromCallback& callback, IUDPSession& session )
	{
		resetOverlapped();
		

		m_pCallback = &callback;
		m_wsabuf.buf = m_buf;
		m_wsabuf.len = sizeof(m_buf);
		m_pSession = &session;
		m_pSession->addRef();

		ZeroMemory(&m_fromSockAddr, sizeof(SOCKADDR_IN));
		m_fromSockAddrSize = sizeof(m_fromSockAddr);
	}

	void YProactorTokenOnRecvFrom::endInit( bool success )
	{
		if (success == false)
		{
			m_pSession->release();
		}
	}

	bool YProactorTokenOnRecvFrom::onCompleted( bool completionStatus, LONG_PTR completionKey, DWORD dwBytesTransfered )
	{
		m_pCallback->onRecved(*m_pSession, YEndPoint(*(sockaddr_in*)&m_fromSockAddr), completionStatus, m_buf, dwBytesTransfered);
		m_pSession->release();
		return true;
	}

	WSABUF& YProactorTokenOnRecvFrom::getWSABuf()
	{
		return m_wsabuf;
	}

	DWORD YProactorTokenOnRecvFrom::getWSABufCount()
	{
		return 1;
	}
	
	SOCKADDR* YProactorTokenOnRecvFrom::getSockAddr()
	{
		return &m_fromSockAddr;
	}

	int* YProactorTokenOnRecvFrom::getSockAddrSize()
	{
		return &m_fromSockAddrSize;
	}

}

